﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 二分查找._35.搜索查找位置
{
    class MainProject
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { -1, 0, 3, 5, 9, 12 };

            Console.WriteLine(SearchInsert(array, 15));
            Console.ReadKey();
        }

        //log(n)的算法：二分查找
        public static int SearchInsert(int[] nums, int target)
        {
            int left = 0, right = nums.Length - 1;
            while (left <= right)
            {
                int mid = left + ((right - left)>>1);//右移操作相当于除以2
                if (nums[mid] == target)
                {
                    return mid;
                }
                else if (nums[mid] < target)
                {
                    left = mid + 1;
                    Console.WriteLine($"left = { left}");
                }
                else
                {
                    right = mid - 1;
                    Console.WriteLine($"right = { right}");
                }
            }
            return left;
        }
    }
}
